﻿/*
Copyright 2007-2008 Matjaž Mešnjak. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/
/*
 * Created by SharpDevelop.
 * User: Matjaz
 * Date: 11.11.2007
 * Time: 14:33
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Data;

using Microsoft.Win32;

namespace gopher
{
	/// <summary>
	/// Description of Dolpoteg.
	/// </summary>
	public partial class Dolpoteg : Form
	{
		public string mojStreznik;
		public string mojSelektor;
		public string mojaVrata;
		//private RegistryKey key;
		
		public Dolpoteg(string selektor, string streznik, string vrata)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();

			mojStreznik = streznik;
			mojSelektor = selektor;
			mojaVrata = vrata;
		}
		
		public byte[] PotegniDol()
		{
			List<byte> mojaDatoteka = new List<byte>();
			try
			{
				TcpClient myClient = new TcpClient();
				myClient.Connect(mojStreznik, Convert.ToInt32(mojaVrata));
				NetworkStream myStream = myClient.GetStream();
				
				String str = mojSelektor+"\r\n";
				ASCIIEncoding asen= new ASCIIEncoding();
				byte[] ba=asen.GetBytes(str);
				myStream.Write(ba,0,ba.Length);
				
				byte[] bb=new byte[1];
				if (myStream.DataAvailable == false) Thread.Sleep(1000);
				while (myStream.DataAvailable)
				{
					int k=myStream.Read(bb,0,bb.Length);
					mojaDatoteka.Add(bb[0]);
					if (myStream.DataAvailable == false) Thread.Sleep(1000);
				}
				myClient.Close();
			}
			catch (Exception ex)
			{
				System.Windows.Forms.MessageBox.Show(ex.ToString(), "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
			}
			return (mojaDatoteka.ToArray());
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			Cursor = System.Windows.Forms.Cursors.WaitCursor;
			System.IO.FileStream fs1=null;
			byte[] b1=null;
			b1 = PotegniDol();			
			fs1=new FileStream(textBox1.Text, FileMode.Create);
			fs1.Write(b1,0,b1.Length);
			fs1.Close();
			fs1 = null;
			Cursor = System.Windows.Forms.Cursors.Default;
			this.Close();
		}
		
//		void Button2Click(object sender, EventArgs e)
//		{
//			string imeDatoteke = mojSelektor.Replace("/","\\");
//			if (imeDatoteke.Contains("\\"))
//			{
//				imeDatoteke = imeDatoteke.Substring(imeDatoteke.LastIndexOf("\\")+1);
//			}
//			
//			folderBrowserDialog1.Description = "Select a folder to which the file will be saved.";
//			
//			if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
//			{
//				textBox1.Text = folderBrowserDialog1.SelectedPath+"\\"+imeDatoteke;
//			}
//		}
		
		void Button3Click(object sender, EventArgs e)
		{
			this.Close();
		}
		
		void ResetFolder()
		{
			RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
			textBox1.Text = key.GetValue("Personal").ToString();
		}
		
		void DolpotegLoad(object sender, EventArgs e)
		{
			string settingsFile;
			RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
			settingsFile = key.GetValue("AppData").ToString()+"\\Gopher\\settings.xml";

			try
			{
				DataSet ds = new DataSet();
				ds.ReadXml(settingsFile);
				DataRow dr = ds.Tables[0].Rows[0];
				textBox1.Text = dr["downloads"].ToString();
				if(textBox1.Text == "")
				{
					ResetFolder();
				}
			}
			catch(Exception)
			{
				ResetFolder();
			}
			
			

			string imeDatoteke = mojSelektor.Replace("/","\\");
			if (imeDatoteke.Contains("\\"))
			{
				imeDatoteke = imeDatoteke.Substring(imeDatoteke.LastIndexOf("\\")+1);				
			}
//			key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
//			String userDocuments = key.GetValue("Personal").ToString();
			textBox1.Text = textBox1.Text+"\\"+imeDatoteke;
		}
		
		void DolpotegHelpRequested(object sender, HelpEventArgs hlpevent)
		{
			Help.ShowHelp(this, Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\"))+"\\Help\\gopher.chm", "html\\download-dialog.htm");
		}
	}
}
